#ifndef UL_MALLOC_POOL_H_ 
#define UL_MALLOC_POOL_H_
#include "ul_memory_pool.h"
#include "ul_mod_define.h"
namespace LinuxNetworkUtility
{
  /// initialize global memory pool
  int ul_init_malloc(UlMemoryPool & global_pool, UlTimer &time, 
                     UlModSet & mod_set, const int64_t thread_pool_gc_frenquency_us);

  /// malloc memory from global memory pool
  void *ul_malloc(const int64_t size, const int32_t mod_id, int64_t *got_size = NULL, 
                  const UlMemoryFreeFlag flag = FREE_TO_THREAD_POOL);

  /// free memory to global memory pool
  void ul_free(void *ptr);
  /// free memory and set ptr to null
  void ul_safe_free(void *& ptr);

  class UlMemBuffer
  {
  public:
    /// default constructor
    UlMemBuffer();
    /// allocate memory from given mempool
    explicit UlMemBuffer(const int64_t nbyte, const int32_t mod_id, 
                         const UlMemoryFreeFlag flag = FREE_TO_THREAD_POOL);
    /// free memory allocated from given mempool
    virtual ~UlMemBuffer();
    /// 重新分配内存
    void *malloc(const int64_t nbyte, const int32_t mod_id, 
                 const UlMemoryFreeFlag flag = FREE_TO_THREAD_POOL);
    /// return memory allocated
    void *get_buffer();
    /// get buffer size
    int64_t get_buffer_size(); 

  private:
    void *buf_ptr_;
    int64_t buf_size_;
  };
}
#endif /// UL_MALLOC_H_
